﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using  System.Web.Security;
using System.DirectoryServices;
using System.Web.Services;
using System.Web.Script.Services; 

namespace WebRSSBuilder.ASP
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonSearch_Click(object sender, EventArgs e)
        {
            if (TextBoxUserName.Text != "")
            {
                this.RoleList.Visible = true; 
                this.RoleList.Items.Clear();
                foreach (String RoleName in Roles.GetAllRoles())
                {
                    ListItem li = new ListItem(); 
                    li.Text = RoleName; 
                    if (Roles.IsUserInRole(TextBoxUserName.Text,RoleName))
                        li.Selected = true; 
                    
                    else
                        li.Selected  = false; 
                   this.RoleList.Items.Add(li); 
                }
                
            }
        }

        protected void LinkButtonSave_Click(object sender, EventArgs e)
        {
            foreach (ListItem li in RoleList.Items)
            {
                if ((li.Selected) && (!Roles.IsUserInRole(TextBoxUserName.Text,li.Text)))
                {
                    Roles.AddUserToRole(this.TextBoxUserName.Text, li.Text); 
                }
                else
                    if ((!li.Selected) && (Roles.IsUserInRole(TextBoxUserName.Text, li.Text)))
                    {
                        Roles.RemoveUserFromRole(this.TextBoxUserName.Text, li.Text);
                    }
            }
            string myscript = "alert('Saved')";
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "MyScript", myscript, true);
        
        }

        protected void LinkButtonCancel_Click(object sender, EventArgs e)
        {
            this.RoleList.Items.Clear();
            this.RoleList.Visible = false;
            this.TextBoxUserName.Text = ""; 
        }
        [WebMethod]
        [ScriptMethod]
        public static string[] GetAllUsers(string prefixText, int count)
        {
            string[] result = { "No Suggestions" };
            /*result = new string[4];
            result[0] = prefixText + "AAAAAAAA";
            result[1] = prefixText + "DDDDDDDD";
            result[2] = prefixText + "MMMMMMMM";
            result[3] = prefixText + "VVVVVVVV";
            */
            return result;
        }
        private DirectoryEntry GetDirectoryObject()
        {
            DirectoryEntry oDE;
            oDE = new DirectoryEntry("LDAP://192.168.1.101", "administrator", "password", AuthenticationTypes.Secure);
            return oDE;
        }
        private List<String> GetADUsers()
        {
            DirectoryEntry de = GetDirectoryObject();
            DirectorySearcher deSearch = new DirectorySearcher();
            deSearch.SearchRoot = de;
            deSearch.Filter = "(objectClass=user)";
            SearchResultCollection results = deSearch.FindAll();
            List<String> l = new List<string>();
            foreach (SearchResult sr in results)
            {

                l.Add(sr.Properties["SAMAccountName"].ToString());
            }
            return l;
        }

       

     
    }
}